﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using Bricks.Business.Service.Impl;
using Bricks.Site.Models;
using Bricks.Business.Models.FlowProcess;
using System.Xml;
using System.Web.Script.Serialization;
using Bricks.Business.Service;
using Bricks.Component.Utils;
using Bricks.Site.Web.CommonUtils;
using Bricks.Component.Utils.Extensions;
using Bricks.Business.Models.ProjectContract;
using Bricks.Business.Service.CommonSupport;

namespace Bricks.Site.Web.Controllers
{
    [Export]
    public class QualityPlanController : BaseController
    {
        [Import]
        public IQualityPlanService QualityPlanService { get; set; }

        [Import]
        public IOrgInfoService OrgInfoService { get; set; }

        [Import]
        public IDocumentService DocumentService { get; set; }

        public ActionResult QualityPlanList()
        {
            ViewBag.ToolBarInfo = base.GetToolBarInfo();
            return View();
        }

        /// <summary>
        /// 获取列表数据
        /// </summary>
        /// <returns></returns>
        public ActionResult GetQualityPlanList(int rows = 20, int page = 1)
        {
            Guid userID = Guid.NewGuid();
            if (SystemData.Instance.CurrentUser != null)
            {
                userID = SystemData.Instance.CurrentUser.ID;
            }
            var queryData = QualityPlanService.QualityPlans.OrderByDescending(m => m.PublishTime).Skip(rows * (page - 1)).Take(rows).ToList();
            var list = from m in queryData
                       select new QualityPlanView
                       {
                           ID = m.ID,
                           PlanName = m.PlanName,
                           PublishTime = m.PublishTime,
                           PublishOrgID = m.PublishOrgID,
                           PublishOrgName = OrgInfoService.GetOrgNameByID(m.PublishOrgID),
                           PublishUserID = m.PublishUserID,
                           PublishUserName = UserInfoService.GetUserNameByID(m.PublishUserID),
                           AbstractDescription = m.AbstractDescription,
                           Attachment = m.Attachment,
                           LoginUserID = userID
                       };
            var result = new { total = QualityPlanService.QualityPlans.Count(), rows = list };
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Delete(string id)
        {
            OperationResult exeResult = new OperationResult(OperationResultType.Success);
            Guid guidID = id.ToGuid();
            var record = QualityPlanService.QualityPlans.FirstOrDefault(m => m.ID == guidID);
            if (record != null)
            {
                //如果当前登录用户不是发布人，则不允许删除
                if (!SystemData.Instance.CurrentUser.ID.Equals(record.PublishUserID))
                {
                    exeResult = new OperationResult(OperationResultType.Warning, "非发布人不允许删除记录");
                }
                else
                {
                    exeResult = QualityPlanService.Delete(guidID);
                    if (exeResult.ResultType == OperationResultType.Success)
                    {
                        var docDeleteResult = DocumentService.DeleteByInstanceID(guidID);
                        if (docDeleteResult == true)
                        {
                            exeResult.Message += "，已删除关联附件记录";
                        }
                    }
                }
            }
            return Json(exeResult);
        }

        /// <summary>
        /// 新增或编辑
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Edit(string id)
        {
            Guid guidID;
            if (!String.IsNullOrEmpty(id))
            {
                guidID = id.ToGuid();
            }
            else
            {
                guidID = Guid.NewGuid();
            }

            QualityPlanView model = QualityPlanService.GetQualityPlan(guidID);
            if (model == null)
            {
                model = new QualityPlanView();
                model.ID = guidID;
                model.PublishTime = DateTime.Now;
                model.PublishUserID = SystemData.Instance.CurrentUser.ID;
                model.PublishUserName = SystemData.Instance.CurrentUser.UserName;
            }
            return View(model);
        }

        /// <summary>
        /// 新增或编辑（响应）
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        [ValidateInput(false)]  //添加这个是为了防止错误：从客户端中检测到有潜在危险的 Request.Form 值
        public ActionResult EditPost(QualityPlanView model)
        {
            return Json(QualityPlanService.SaveQualityPlan(model), "text/html");
        }

        public ActionResult Preview(string id)
        {
            QualityPlanView model = QualityPlanService.GetQualityPlan(id.ToGuid());
            return View(model);
        }

        /// <summary>
        /// 预览详细通报
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Detail(string id)
        {
            return Preview(id);
        }
    }
}